﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DAL.Mappers;
using Frw;
using Frw.DAL;

namespace ServiceLayer
{
    [Context(typeof(BetpointAdvancedEntities))]
    public class UserAuthentication : FacadeBase<IAuthentication, UserAuthentication>, IAuthentication
    {
        ICollection<System.ComponentModel.DataAnnotations.ValidationResult> results = null;

        public UserAuthentication()
        {
            
        }

        public bool ValidateUser(BLL.User user)
        {
            bool retValue = false;

            try
            {
                Console.WriteLine(string.Format("Validazione utente: {0}", user.Name));

                retValue = this.Validator.Validate<BLL.User>(user);
                if (!retValue)
                {
                    this.Validator.Results.ToList().ForEach(x => Console.WriteLine(x.ErrorMessage));
                }

                Logger.Log(LogLevel.Fatal, "Prova di Logging ERRORE");
            }
            catch (Exception ex)
            {

            }

            return retValue;
        }

        public BLL.User GetUser(BLL.User user)
        {
            BLL.User retValue = null;

            var obj = from x in ((BetpointAdvancedEntities)this.GetContext()).utenti where x.id_utente == user.Id select x;

            UserMapper um = new UserMapper();
            retValue = um.ToDTO(obj).FirstOrDefault();

            //Console.WriteLine(string.Format("Creazione utente: {0}", user.Name));

            return retValue;
        }
    }
}
